import Vue from 'vue'
import VueRouter from 'vue-router'
import * as hooks from './hooks'

const moduleFn = require.context('./routes', false, /\.js$/i)
const routes = moduleFn.keys().reduce((prev, curr) => {
  let module = moduleFn(curr).default
  if (Array.isArray(module)) {
    prev.push(...module)
  } else {
    prev.push(module)
  }
  return prev
}, [])

Vue.use(VueRouter)


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

Object.keys(hooks).forEach(fn => router.beforeEach(hooks[fn]))


// vue-router@3/4 都存在的方法
// router.addRoute(对象)
// vue-router@4  不存在了
// router.addRoutes([对象])


export default router